#include<bits/stdc++.h>
using namespace std;
const int N=2*1e5+10;

int tire[N][26];
int pass[N];
int end1[N];
int cnt;
void insert(string& s)
{
    int cur=0;
    pass[cur]++;//每插入一个代表经过根节点一次
    for(auto&ch:s)
    {
        int path=ch-'a';
        if(tire[cur][path]==0)
        {
            tire[cur][path]=++cnt;
            pass[cnt]++;
        }
        cur=tire[cur][path];
    }
    end1[cur]++;
}
int find(string&s)
{
    //从根节点便利
    int cur=0;
    for(auto&ch:s)
    {
        int path=ch-'a';
        if(tire[cur][path]==0) return 0;
        cur=tire[cur][path];
    }
    return end1[cur];
}
int main()
{
    int n;cin>>n;
    while(n--)
    {
        string a,b;
        cin>>a>>b;
        if(a==string("I")) insert(b);
        else cout<<find(b)<<endl;
    }
    return 0;
}



